package com.quartz.controller;

import com.quartz.entity.AppQuartz;
import com.quartz.entity.JobAndTrigger;
import com.quartz.util.QuartzUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-02 11:29
 **/
@RestController
@RequestMapping("/myQuartz")
public class MyQuartzController{

    private static Logger log = LoggerFactory.getLogger(MyQuartzController.class);

    @Autowired
    private QuartzUtil quartzUtil;

    @RequestMapping(value = "/queryJob", method = RequestMethod.GET)
    public List<JobAndTrigger> queryJob(){
        List<JobAndTrigger> result = quartzUtil.jobList();
        return result;
    }

    @RequestMapping(value = "/addJob", method = RequestMethod.PUT)
    public boolean addJob(@RequestBody AppQuartz appQuartz){
        try {
            quartzUtil.addJob(appQuartz);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "/modifyJob", method = RequestMethod.PUT)
    public boolean modifyJob(@RequestBody AppQuartz appQuartz){
        try {
            quartzUtil.modifyJob(appQuartz);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "/deleteJob", method = RequestMethod.DELETE)
    public boolean deleteJob(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobDelete(jobName, jobGroup);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "/jobPause", method = RequestMethod.PUT)
    public boolean jobPause(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobPause(jobName, jobGroup);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "/jobResume", method = RequestMethod.PUT)
    public boolean jobResume(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobResume(jobName, jobGroup);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



}
